package com.project.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.mapper.DishCategoryMapper;
import com.project.server.model.Dish;
import com.project.server.model.DishCategory;
import com.project.server.service.DishCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
@RequiredArgsConstructor
public class DishCategoryServiceImpl implements DishCategoryService {

    @Autowired
    private DishCategoryMapper categoryMapper;

    @Override
    public Page<DishCategory> page(Integer pageNum, Integer pageSize, String name, Long categoryId, Long restaurantId, Integer status) {
        Page<DishCategory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DishCategory> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(name)) {
            wrapper.like(DishCategory::getName, name);
        }
        if (restaurantId != null) {
            wrapper.eq(DishCategory::getRestaurantId, restaurantId);
        }
        if (status != null) {
            wrapper.eq(DishCategory::getStatus, status);
        }

        wrapper.orderByAsc(DishCategory::getSort);
        return categoryMapper.selectPage(page, wrapper);
    }

    @Override
    public DishCategory getById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void save(DishCategory dishCategory) {
        categoryMapper.insert(dishCategory);
    }

    @Override
    public void update(DishCategory dishCategory) {
        categoryMapper.updateById(dishCategory);
    }

    @Override
    public void removeById(Long id) {
        categoryMapper.deleteById(id);
    }


    @Override
    public List<DishCategory> getList(Long restaurantId){
        return categoryMapper.selectList(new LambdaQueryWrapper<DishCategory>()
                .eq(DishCategory::getRestaurantId,restaurantId));
    }





}
